package com.yc.vote.servlet;

import com.google.gson.Gson;
import com.yc.vote.biz.BizException;
import com.yc.vote.biz.TpVoteBiz;
import com.yc.vote.entity.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "saveVoteServlet", value = "/saveVote")
public class saveVoteServlet extends HttpServlet {

    TpVoteBiz biz = new TpVoteBiz();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String vname = request.getParameter("vname");
        String vtype = request.getParameter("vtype");
        String[] inames = request.getParameterValues("iname");
        Result result;
        try {
            biz.save(vname,vtype,inames);
            result = new Result(1,"添加成功",null);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0,e.getMessage(),null);
        }
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().append(new Gson().toJson(result));
    }
}
